package org.example.domain.strategy.service.rule.chain.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.strategy.repository.IStrategyRepository;
import org.example.domain.strategy.service.rule.chain.AbstractLogicChain;
import org.example.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import org.example.types.common.Constants;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component("rule_blacklist")
public class BackListLogicChain extends AbstractLogicChain {
    @Resource
    private IStrategyRepository repository;

    protected String ruleModel = DefaultChainFactory.LogicModel.RULE_BLACK.getCode();
    @Override
    public DefaultChainFactory.StrategyAwardVO logic(String userId, Long strategyId) {
        log.info("规则责任链-黑名单 userId:{} strategyId:{} ruleModel:{}", userId,strategyId,ruleModel );

        //查询规则配置
        String ruleValue = repository.queryStrategyRuleValue(strategyId, ruleModel);
        String[] splitruleValue = ruleValue.split(Constants.COLON);
        Integer awardId = Integer.parseInt(splitruleValue[0]) ;

        //黑名单中奖判断
        String[] userBlackIds = splitruleValue[1].split(Constants.SPLIT);
        for (String userBlackId:userBlackIds) {
            if (userId.equals(userBlackId)){
            log.info("规则责任链-黑名单接管 userId:{} strategyId:{} ruleModel:{} awardId:{}", userId,strategyId,ruleModel,awardId);
            return DefaultChainFactory.StrategyAwardVO.builder()
                    .awardId(awardId)
                    .logicModel(DefaultChainFactory.LogicModel.RULE_BLACK)
                    .build();
            }
        }
        //过滤其他责任链
            log.info("规则责任链-黑名单放行 userId:{} strategyId:{} ruleModel:{} awardId:{}", userId,strategyId,ruleModel,awardId);

            return next().logic(userId,strategyId);
    }

}
